package com.cqc.datepickerdialog01;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button btn1;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;
        btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                                showDatePickerDialog();
//                showTimePickerDialog();
            }
        });
    }

    private void showDatePickerDialog() {
        Calendar calendar = Calendar.getInstance();
        int weekYear = calendar.get(Calendar.YEAR);
        int monthOfYear = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog.OnDateSetListener setListener = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                Log.d(TAG, "year=" + year + ",month=" + (month+1) + ",day=" + day);
            }
        };
        final DatePickerDialog datePickerDialog = new DatePickerDialog(context, setListener, weekYear, monthOfYear, dayOfMonth);
        //截至到这里会走 onDateSet方法，但是下面就 不会走这个方法了。


        //        datePickerDialog.setButton(DatePickerDialog.BUTTON_POSITIVE, "选择时间", new DialogInterface.OnClickListener() {
        //            @Override
        //            public void onClick(DialogInterface dialogInterface, int i) {
        //                DatePicker datePicker = datePickerDialog.getDatePicker();
        //                int year = datePicker.getYear();
        //                int month = datePicker.getMonth() + 1;
        //                int dayOfMonth = datePicker.getDayOfMonth();
        //                Log.d(TAG, "year=" + year + ",month=" + month + ",dayOfMonth=" + dayOfMonth);
        //                showTimePickerDialog();
        //            }
        //        });
        datePickerDialog.setButton(DatePickerDialog.BUTTON_POSITIVE, "选择时间", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
//                            DatePicker datePicker = datePickerDialog.getDatePicker();
//                            int year = datePicker.getYear();
//                            int month = datePicker.getMonth() + 1;
//                            int dayOfMonth = datePicker.getDayOfMonth();
//                            Log.d(TAG, "year=" + year + ",month=" + month + ",dayOfMonth=" + dayOfMonth);
                            showTimePickerDialog();
                        }
                    });
        datePickerDialog.setButton(DatePickerDialog.BUTTON_NEGATIVE, "", datePickerDialog);
        datePickerDialog.show();
    }

    private String time;

    private void showTimePickerDialog() {
        Calendar calendar = Calendar.getInstance();
        int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        Log.d(TAG, "hourOfDay=" + hourOfDay + ",hour=" + hour + ",minute=" + minute);
        TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int hour, int minute) {
                Log.d(TAG, "hour=" + hour + ",minute=" + minute);
                time = hour + "" + minute;
            }
        };
        final TimePickerDialog timePickerDialog = new TimePickerDialog(context, onTimeSetListener, hourOfDay, minute, false);
        timePickerDialog.setButton("确定", timePickerDialog);
        Log.d(TAG, "time=" + time);
        timePickerDialog.setButton2("设置日期", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                showDatePickerDialog();
            }
        });
        timePickerDialog.show();
    }

}
